/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "../../stdafx.h"
#include "../../include/scroll/FCHScrollBar.h"

namespace FaceCat{
	FCHScrollBar::FCHScrollBar(){
	}

	FCHScrollBar::~FCHScrollBar(){
		FCButton *backButton = getBackButton();
		if(backButton){
			backButton->removeEvent((FCTouchEventCallBack*)this, L"ontouchdown");
			backButton->removeEvent((FCTouchEventCallBack*)this, L"ontouchup");
		}
	}

	void FCHScrollBar::callTouchEvent(String eventName, Object sender, FCTouchInfo touchInfo, Object invoke){
		FCScrollBar::callTouchEvent(eventName, sender, touchInfo, invoke);
        if (sender == getBackButton()) {
			if (eventName == L"ontouchdown") {
                onBackButtonTouchDown(touchInfo);
			} else if (eventName == L"ontouchup") {
                onBackButtonTouchUp(touchInfo);
            }
        }
	}

	String FCHScrollBar::getViewType(){
		return L"HScrollBar";
	}

	void FCHScrollBar::onBackButtonTouchDown(FCTouchInfo touchInfo){
		FCButton *scrollButton = getScrollButton();
		FCPoint mp = touchInfo.m_firstPoint;
		if (mp.x < scrollButton->getLeft()){
			pageReduce();
			setIsReducing(true);
		}
		else if (mp.x > scrollButton->getRight()){
			pageAdd();
			setIsAdding(true);
		}
	}

	void FCHScrollBar::onBackButtonTouchUp(FCTouchInfo touchInfo){
		 setIsAdding(false);
         setIsReducing(false);
	}

	void FCHScrollBar::onDragScroll(){
		bool floatRight = false;
		FCButton *backButton = getBackButton();
		FCButton *scrollButton = getScrollButton();
		int backButtonWidth = backButton->getWidth();
		int contentSize = getContentSize();
		if (scrollButton->getRight() > backButtonWidth){
			floatRight = true;
        }
		FCScrollBar::onDragScroll();
		if(floatRight){
			setPos(contentSize);
		}
		else{
			setPos((int)((Long)contentSize * (Long)scrollButton->getLeft() / backButtonWidth));
		}
		onScrolled();
	}

	void FCHScrollBar::onLoad(){
		bool isAdd = false;
		FCButton *backButton = getBackButton();
		if (backButton){
			isAdd = true;
		}
		FCScrollBar::onLoad();
		if (!isAdd){
			backButton = getBackButton();
			backButton->addEvent((FCTouchEventCallBack*)this, L"ontouchdown", this);
			backButton->addEvent((FCTouchEventCallBack*)this, L"ontouchup", this);
		}
	}

	void FCHScrollBar::update(){
		if(m_native && !m_native->m_lockUpdate){
			FCButton *addButton = getAddButton();
			FCButton *backButton = getBackButton();
			FCButton *reduceButton = getReduceButton();
			FCButton *scrollButton = getScrollButton();
			int width = getWidth(), height = getHeight(), contentSize = getContentSize();
			if (contentSize > 0 && addButton && backButton && reduceButton && scrollButton ){
				int pos = getPos();
				int pageSize = getPageSize();
				if (pos > contentSize - pageSize) {
					pos = contentSize - pageSize;
					m_pos = pos;
				}
				if (pos < 0) {
					pos = 0;
					m_pos = pos;
				}
				int abWidth = addButton->isVisible() ? addButton->getWidth() : 0;
				FCSize aSize = {abWidth, height};
				addButton->m_size = aSize;
				FCPoint aPoint = {width - abWidth, 0};
				addButton->m_location = aPoint;
				int rbWidth = reduceButton->isVisible() ? reduceButton->getWidth() : 0;
				FCSize sSize = {rbWidth, height};
				reduceButton->m_size = sSize;
				FCPoint sPoint = {0, 0};
				reduceButton->m_location = sPoint;
				int backWidth = width - abWidth - rbWidth;
				FCSize bSize = {backWidth, height};
				backButton->m_size = bSize;
				FCPoint bPoint = {rbWidth, 0};
				backButton->m_location = bPoint;
				int scrollWidth = backWidth * pageSize / contentSize;
				int scrollPos = (int)((Long)backWidth * (Long)pos / contentSize);
				if(scrollWidth < 10){
					scrollWidth = 10;
					if (scrollPos + scrollWidth > backWidth){
						scrollPos = backWidth - scrollWidth;
					}
				}
				FCSize scSize = {scrollWidth, height};
				scrollButton->m_size = scSize;
				FCPoint scPoint = {scrollPos, 0};
				scrollButton->m_location = scPoint;
			}
		}
	}
}
